package com.ftg.exam.manage.controller;

import com.ftg.exam.manage.pojo.Difficulties;
import com.ftg.exam.manage.service.DifficultiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zwl
 * Create on 2019/9/8 15:14
 * function
 */
@RestController
@CrossOrigin
@RequestMapping("/tree")
public class TreeController {
    @Autowired
    DifficultiesService  ds;


    //获取树型结构
    @ResponseBody
    @RequestMapping("/local")
    public Object localData() {
        List<Difficulties> difficulties = new ArrayList<Difficulties>();
        Difficulties parent=new  Difficulties();
        parent.setId(0);

        queryChildDifficulties(parent);
        return parent.getChildren();
    }
    //递归
    private void queryChildDifficulties(Difficulties parent){
        List<Difficulties> child=ds.childDifficulties(parent.getId());
        for(Difficulties difficulties:child){
            queryChildDifficulties(difficulties);
        }
        parent.setChildren(child);
    }
        @PostMapping("/update")
    public int update(@RequestBody Difficulties difficulties){
        return ds.update(difficulties);
    }

    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable("id") String id){
        return ds.delete(id);
    }

    @PostMapping("/save")
    public int save(@RequestBody Difficulties difficulties){
        return ds.save(difficulties);
    }
}
